package hlhj.fhp.supreme.activitys

import android.annotation.SuppressLint
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Message
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import fhp.hlhj.giantfold.utils.LogUtil
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.javabean.BaseBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import kotlinx.android.synthetic.main.activity_forget_aty.*

class ForgetAty : BaseAty() {
    private var sec=60
    private val  handler= @SuppressLint("HandlerLeak")
    object : Handler(){
        override fun handleMessage(msg: Message?) {
            super.handleMessage(msg)
            if (sec>0){
                sec--
                btSms.text="$sec s"
                this.sendEmptyMessageDelayed(1,1000)
            }else{
                btSms.text="验证码"
                btSms.isClickable=true
            }
        }
    }
    override fun getContentId(): Int {
        return R.layout.activity_forget_aty

    }

    override fun initListener() {
        btExit.setOnClickListener { finish() }
        btSms.setOnClickListener {
            if (etPhone.text.toString().length==11){
                LogUtil.log("点击了！！正确")
                OkGo.post<BaseBean>(Urls.BaseUrl+ Urls.SEND_SMS)
                        .params("mobile",etPhone.text.toString())
                        .execute(object : JsonCallBack<BaseBean>(){
                            override fun onSuccess(response: Response<BaseBean>?) {
                                val bean = response?.body()
                                if (bean?.code==1){
                                    MyUtils.toast("发送成功")
                                    btSms.text="$sec s"
                                    btSms.isClickable=false
                                    handler.sendEmptyMessage(1)
                                }
                            }
                        })
            }else{
                MyUtils.toast("手机号不正确")
            }
        }


        btCommit.setOnClickListener {

            if (etPhone.text.toString().length==0||etPwd.text.toString().length==0||etYzm.text.toString().length==0){
                if (etPhone.text.isEmpty()){
                    MyUtils.toast("请填写手机号")
                    return@setOnClickListener
                }

                if (etYzm.text.isEmpty()){
                    MyUtils.toast("请填写验证码")
                    return@setOnClickListener
                }

                if (etPwd.text.isEmpty()){
                    MyUtils.toast("请填写密码")
                    return@setOnClickListener
                }

            }else{
                OkGo.post<BaseBean>(Urls.BaseUrl+Urls.FORGET)
                        .params("mobile",etPhone.text.toString())
                        .params("captcha",etYzm.text.toString())
                        .params("user_pass",etPwd.text.toString())
                        .execute(object :JsonCallBack<BaseBean>(){
                            override fun onSuccess(response: Response<BaseBean>?) {
                                val bean = response?.body()
                                MyUtils.toast(bean?.msg)
                                if (bean?.code==1){
                                    finish()
                                }
                            }
                        })
            }
        }
    }




}
